package com.edata.bigdata.util

import org.apache.spark.SparkContext
import org.apache.spark.broadcast.Broadcast
import spire.ClassTag

class BroadcastWrapper[T: ClassTag] extends Serializable {
  @volatile var instance: Broadcast[T] = _

  def getInstance(sc: SparkContext, data: T): Broadcast[T] = {
    if (instance == null) {
      synchronized {
        if (instance == null) {
          instance = sc.broadcast(data)
        }
      }
    }
    instance
  }

  def updateBroadCastVar(sc: SparkContext, data: T): Unit = {
    if (instance == null) {
      synchronized {
        instance.unpersist(true)
        instance = sc.broadcast(data)
      }

    }
  }
}
